package com.one.blocks.mvc.convert;

import com.one.blocks.mvc.controller.vo.GrantVo;
import com.one.blocks.mvc.domain.GrantDomain;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author <a href="mailto:idler41@163.con">linfuxin</a> created on 2024-01-04 15:02:30
 */
public interface GrantConvert<E extends GrantDomain<I, K, ID>, I, K, ID extends Serializable> {

    default List<E> grantVoToGrantEntity(GrantVo<I, K> grantVo) {
        if (CollectionUtils.isEmpty(grantVo.getRelatedIds())) {
            return Collections.emptyList();
        }
        List<E> dataToInsert = new ArrayList<>(grantVo.getRelatedIds().size());
        for (K relatedId : grantVo.getRelatedIds()) {
            E entity = newInstance();
            entity.setGrantId(grantVo.getGrantId());
            entity.setRelatedId(relatedId);
            entity.setCreateId(getCreateId());
            entity.setCreateTime(getCreateTime());
            dataToInsert.add(entity);
        }
        return dataToInsert;
    }

    E newInstance();

    ID getCreateId();

    LocalDateTime getCreateTime();
}
